package cn.com.hhrcw.base;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import org.jeecg.common.util.SpringContextUtils;

import javax.servlet.http.HttpServletRequest;
import java.beans.Introspector;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @author admin
 */
@Data
public class BaseParams {
    /**
     * 页码
     */
    private Integer pageNo = 1;

    /**
     * 页大小
     */
    private Integer pageSize = 10;

    /**
     * 排序字段
     */
    private String field;

    /**
     * 排序方式 true:正序 false:倒序
     */
    private Boolean sort;

    public IPage page() {
        IPage<?> page = new Page();
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        return page;
    }

    private <T extends BaseParams> String getFieldName(SFunction<T, ?> sfun) {
        String fieldName = null;
        try {
            // 第1步 获取SerializedLambda
            Method method = sfun.getClass().getDeclaredMethod("writeReplace");
            method.setAccessible(Boolean.TRUE);
            SerializedLambda serializedLambda = (SerializedLambda) method.invoke(sfun);
            // 第2步 implMethodName 即为Field对应的Getter方法名
            String implMethodName = serializedLambda.getImplMethodName();
            String get = "get";
            int a = 3;
            String is = "is";
            int b =2;
            String lambda = "lambda$";
            if (implMethodName.startsWith(get) && implMethodName.length() > a) {
                fieldName = Introspector.decapitalize(implMethodName.substring(3));

            } else if (implMethodName.startsWith(is) && implMethodName.length() > b) {
                fieldName = Introspector.decapitalize(implMethodName.substring(2));
            } else if (implMethodName.startsWith(lambda)) {
                throw new IllegalArgumentException("SerializableFunction不能传递lambda表达式,只能使用方法引用");

            } else {
                throw new IllegalArgumentException(implMethodName + "不是Getter方法引用");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fieldName;
    }

    /**
     * 开始时间
     * @param sFunction
     * @param <T>
     * @return
     */
    public <T extends  BaseParams>Date begin(SFunction<T, Date> sFunction){
        String fieldName = this.getFieldName(sFunction);
        return parse(fieldName,"_begin");
    }

    /**
     * 结束时间
     * @param sFunction
     * @param <T>
     * @return
     */
    public <T extends  BaseParams>Date end(SFunction<T, ?> sFunction){
        String fieldName = this.getFieldName(sFunction);
        return parse(fieldName,"_end");
    }

    private Date parse(String fieldName, String suffix){
        HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
        String parameter = request.getParameter(fieldName + suffix);
        return DateUtil.parse(parameter);
    }
}
